package com.ytf.test.eighth;

class Useful{
    void f(){
        System.out.println("Useful.f()");
    }
    void g(){
        System.out.println("Useful.g()");
    }
}
class MoreUseful extends Useful{
    @Override
    void f() {
        System.out.println("MoreUseful.f()");
    }

    @Override
    void g() {
        System.out.println("MoreUseful.g()");
    }
    void u(){
        System.out.println("MoreUseful.u()");
    }
    void v(){}
    void w(){}
}

/**
 * @author: YTF
 * @date: 2025/1/25 10:25
 * @version: 1.0
 * @description:
 */
public class RTTI {
    public static void main(String[] args) {
        Useful[] useful={new Useful(),new MoreUseful()};
        useful[0].f();
        useful[1].g();
        // Compile time: method not found in Useful
        //! useful[1].u();
        ((MoreUseful)useful[1]).u(); //Downcast/RTTI
        ((MoreUseful)useful[0]).u(); //Exception thrown

        /**
         * OutPut:
         * Useful.f()
         * MoreUseful.g()
         * MoreUseful.u()
         * Exception in thread "main" java.lang.ClassCastException: com.ytf.test.eighth.Useful cannot be cast to com.ytf.test.eighth.MoreUseful
         * 	at com.ytf.test.eighth.RTTI.main(RTTI.java:42)
         *
         * 总结：
         * 向上转型后无法调用子类中独有的方法，如果需要调用需要进行向下转型，但向下转型可能会出现转型异常 RTTI ClassCastException
         */
    }
}
